package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.intervention_details.bean.ImproveDetailsBean;
import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.DrawableUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/14.
 * 个人健康信息汇总 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class HealthImproveDetailsAdapter extends RecyclerView.Adapter {
    private static final int TYPE_HEAD_INFO = 0;
    private static final int TYPE_DANGER_TIP = 1;
    private static final int TYPE_DANGER_IMPROVE_CONTENT = 2;
    private static final int TYPE_FOOTER = 3;
    private ImproveDetailsBean mBean;
    private Context mContext;

    public HealthImproveDetailsAdapter(Context context, ImproveDetailsBean bean) {
        this.mContext = context;
        this.mBean = bean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_HEAD_INFO:
                view = LayoutInflater.from(mContext).inflate(R.layout.scheme_common_head_info, parent, false);
                return new HeadInfoHolder(view);
            case TYPE_DANGER_TIP:
                view = LayoutInflater.from(mContext).inflate(R.layout.user_health_info_list_item3, parent, false);
                return new DangerTipHolder(view);
            case TYPE_DANGER_IMPROVE_CONTENT:
                view = LayoutInflater.from(mContext).inflate(R.layout.common_layout_recycler, parent, false);
                return new DangerImproveContentHolder(view);
            case TYPE_FOOTER:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_foot_details, parent, false);
                return new FooterHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadInfoHolder) {
            HeadInfoHolder headInfoHolder = (HeadInfoHolder) holder;
            headInfoHolder.tvSchemeCommonHeadTitle.setText("健康行动改善指南");
            headInfoHolder.tvSchemeCommonHeadContent.setText(mBean.getGuide());
        }

        if (holder instanceof DangerTipHolder) {
            DangerTipHolder dangerTipHolder = (DangerTipHolder) holder;
        }

        if (holder instanceof DangerImproveContentHolder) {
            DangerImproveContentHolder improveHolder = (DangerImproveContentHolder) holder;
            DangerImproveContentAdapter adapter = new DangerImproveContentAdapter(mContext,mBean.getList(),mBean.getSuggest());
            improveHolder.recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD_INFO;
            case 1:
                return TYPE_DANGER_TIP;
            case 2:
                return TYPE_DANGER_IMPROVE_CONTENT;
            case 3:
                return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }


    class HeadInfoHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_scheme_common_head_title)
        TextView tvSchemeCommonHeadTitle;
        @Bind(R.id.tv_scheme_common_head_content)
        TextView tvSchemeCommonHeadContent;

        public HeadInfoHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class DangerTipHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_subtitle)
        TextView tvSubtitle;
        @Bind(R.id.my_flow_layout)
        MyFlowLayout myFlowLayout;

        public DangerTipHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            initView();
        }

        private void initView() {
            tvSubtitle.setText("危险因素提示");
            initTipTextView(myFlowLayout);
        }

        private void initTipTextView(MyFlowLayout flowLayout) {
            int padding = DensityUtil.dip2px(mContext, 12);
            for (int i = 0; i < mBean.getRiskfactor().size(); i++) {
                final String keyword = mBean.getRiskfactor().get(i);
                TextView view = new TextView(mContext);
                view.setText(keyword);
                view.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);// 18sp
                view.setGravity(Gravity.CENTER);

                // 生成随机颜色
                Random random = new Random();
                int r = 30 + random.nextInt(200);
                int g = 30 + random.nextInt(200);
                int b = 30 + random.nextInt(200);
                view.setTextColor(Color.rgb(r, g, b));
                GradientDrawable bgNormal = DrawableUtils.getGradientDrawable(Color.rgb(r, g, b), DensityUtil.dip2px(mContext, 20));
                view.setBackgroundDrawable(bgNormal);
                view.setPadding(padding, DensityUtil.dip2px(mContext, 3), padding, DensityUtil.dip2px(mContext, 3));
                ViewGroup.MarginLayoutParams marginLayoutParams = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                marginLayoutParams.setMargins(DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 10), DensityUtil.dip2px(mContext, 5), DensityUtil.dip2px(mContext, 10));// childView的间距
                flowLayout.addView(view, marginLayoutParams);
            }
        }
    }


    class DangerImproveContentHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.recycler_view_common)
        RecyclerView recyclerView;

        public DangerImproveContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setFocusable(false);
        }
    }

    class FooterHolder extends RecyclerView.ViewHolder {

        public FooterHolder(View itemView) {
            super(itemView);
        }
    }


}
